<template>
    <el-dialog ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="50%"
        :before-close="handleClose" v-loading="loading">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item v-if="this.addEditFlag == 'edit'" label="文件名" prop="fileName">
                <el-input class="w100p" v-model="ruleForm.fileName" placeholder="请输入文件名"></el-input>
            </el-form-item>
            <el-form-item label="选择功能" prop="deviceTypeId" v-if="addEditFlag=='add'">
                <el-select v-model="ruleForm.deviceTypeId" class="w100p" clearable filterable placeholder="请选择设备编码">
                    <el-option v-for="item, index in deviceTypeList" :label="item.deviceTypeName" :value="item.deviceTypeId"
                        :key="index"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="上传文件" v-if="addEditFlag=='add'">
                <el-upload class="upload" action="#" :file-list="fileList" :show-file-list="false" :limit="1"
                        :multiple=false :auto-upload="false" :on-change="handleChange">
                        <el-button type="text" :disabled="!ruleForm.deviceTypeId">上传文件</el-button>
                    </el-upload>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer" v-show="addEditFlag=='edit'">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>

import files from "@/api/files"
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        deviceTypeList: {
            type: Array,
            default: () => {
                return []
            }
        },
    },
    data() {
        return {
            loading:false,
            fileList:[],
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {
                fileName: '',
                deviceTypeId: ''
            },
            rules: {
                deviceTypeId: [
                    { required: true, message: '请选择类型', trigger: 'change' }
                ],
                fileName: [
                    { required: true, message: '请输入文件名称', trigger: 'change' }
                ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },
    computed: {},
    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = ''
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = files.edit
                this.addEditName = '编辑'
            }
        },
        handleChange(file, fileList) {
            console.log(fileList)
            let formData = new FormData();
            formData.append("file", file.raw);
            formData.append('fileName', file.name);
            formData.append('deviceTypeId', this.ruleForm.deviceTypeId);
            this.loading = true;
            files.upload(formData).then(res => {
                this.loading = false;
                if (res.code == 1) {
                    this.$message.success('上传成功！')
                    this.cancel();
                } else {
                    this.$message.success(res.msg || '上传失败！')
                }
            }).catch(err => {
                this.loading = false;
                console.log(err)
            })
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    if (this.addEditApi) {
                        this.addEditApi(this.ruleForm).then(res => {
                            if (res.code == 1) {
                                this.cancel();
                                this.$message.success('编辑成功')
                            } else {
                                this.$message.warning(res.msg || '编辑失败')
                            }
                        })
                    } else {
                        this.$emit('submitFile', this.ruleForm)
                    }
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.fileList =[];
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.line {
    border-bottom: 1px solid #e6e6e6;
}

.icon {
    font-size: 20px;
    line-height: 29px;
    cursor: pointer;
}

::v-deep .noLabel {
    .el-form-item {
        .label {
            display: none;
        }

        .el-form-item__content {
            margin: 0;
        }
    }
}
</style>